/*AyflEomXWyqCHfdTxzWda+ZiNDhxOZJeZF/oshAh0uo=*/
/*
 * @Author: xiahui
 * @PageInfo: 合同控制范围组件
 * @Date: 2019-02-25 11:30:42
 * @Last Modified by: CongKe
 * @Last Modified time: 2019-12-23 11:03:48
 */
import React, { Component } from 'react';
import { base, high, ajax } from 'nc-lightapp-front';
import './index.less';
import { initLang, getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';
import { showWarningInfo } from '../../../../../scmpub/scmpub/pub/tool/messageUtil';

const { NCButton, NCModal } = base;
const { Header, Title, Body, Footer } = NCModal;
const { Transfer } = high;

const URL = {
	controlScope: '/nccloud/ct/purdaily/controlScope.do',
	controlScope_ok: '/nccloud/ct/purdaily/controlScope_ok.do',
	batchControlScope: '/nccloud/ct/purdaily/batchControlScope.do',
	batchControlScope_add: '/nccloud/ct/purdaily/batchControlScope_add.do',
	batchControlScope_delete: '/nccloud/ct/purdaily/batchControlScope_delete.do',
};

class ControlScopeDlg extends Component {
	constructor() {
		super();
		this.state = {
			dataSource: [],
			targetKeys: [],
			show: false,
		};
		this.pks; // 批量合同控制范围可编辑的Pks
		this.dataKeys = []; // 数据源关键字
		initLang(this, ['4004pub'], 'ct');
	}

	componentWillReceiveProps(nextProps) {
		let { show, pk, isBatch } = nextProps;
		if (show && pk) {
			// 设置界面初始数据
			ajax({
				url: isBatch ? URL.batchControlScope : URL.controlScope,
				data: pk,
				success: res => {
					if (res.success && res.data) {
						this.dataKeys = res.data['dataKeys'];
						this.pks = res.data['pks'];
						let dataSource = [];
						res.data['dataSource'].forEach((value, index) => {
							dataSource.push({ key: index, title: value, disabled: res.data['disabled'] });
						});
						this.setState({
							dataSource: dataSource,
							targetKeys: res.data['targetKeys'].map(Number),
							show: true,
						});
					}
				},
				error: res => {
					showWarningInfo(res.message);
					this.state.show = false;
					this.props.close();
				},
			});
		} else {
			this.state.show = false;
		}
	}

	onTargetKeysChange = targetKeys => {
		this.setState({
			targetKeys,
		});
	};

	render() {
		let { pk, isBatch } = this.props;
		let { dataSource, targetKeys } = this.state;
		return (
			<NCModal
				show={this.state.show}
				onHide={this.props.close}
				className="control-scope-transfer"
				fieldid="bill_controlscope"
			>
				<Header closeButton>
					<Title>
						{isBatch ? getLangByResId(this, '4004PUB-000000') : getLangByResId(this, '4004PUB-000001')}
					</Title>
					{/* 国际化处理： 合同控制范围批量调整,合同控制范围*/}
				</Header>
				<Body>
					<div>
						<Transfer
							{...{
								dataSource,
								targetKeys,
								showSearch: false,
								titles: [
									getLangByResId(this, '4004PUB-000002'),
									isBatch
										? getLangByResId(this, '4004PUB-000003')
										: getLangByResId(this, '4004PUB-000001'),
								] /* 国际化处理： 用户关联组织,待调整的合同控制范围,合同控制范围*/,
								onTargetKeysChange: this.onTargetKeysChange,
								lazy: { container: 'modal' },
							}}
						/>
					</div>
				</Body>
				<Footer>
					{isBatch ? (
						<div>
							<NCButton onClick={addBtnClick.bind(this)} fieldid="Add">
								{getLangByResId(this, '4004PUB-000004')}
							</NCButton>
							{/* 国际化处理： 增加*/}
							<NCButton onClick={deleteBtnClick.bind(this)} fieldid="Del">
								{getLangByResId(this, '4004PUB-000005')}
							</NCButton>
							{/* 国际化处理： 删除*/}
							<NCButton onClick={this.props.close} fieldid="Cancel">
								{getLangByResId(this, '4004PUB-000006')}
							</NCButton>
							{/*
							国际化处理： 取消*/}
						</div>
					) : (
						<div>
							<NCButton onClick={beSureBtnClick.bind(this, pk)} className="button-primary" fieldid="Sure">
								{getLangByResId(this, '4004PUB-000007')}
								{/* 国际化处理： 确定*/}
							</NCButton>
							<NCButton onClick={this.props.close} fieldid="Cancel">
								{getLangByResId(this, '4004PUB-000006')}
							</NCButton>
							{/* 国际化处理： 取消*/}
						</div>
					)}
				</Footer>
			</NCModal>
		);
	}
}
/**
 * 合同控制范围批量调整，增加操作
 */
function addBtnClick() {
	commonBtnClick.call(this, URL.batchControlScope_add, this.pks);
}

/**
 * 合同控制范围批量调整，删除操作
 */
function deleteBtnClick() {
	commonBtnClick.call(this, URL.batchControlScope_delete, this.pks);
}

/**
 * 合同控制范围, 确认操作
 */
function beSureBtnClick(pk) {
	commonBtnClick.call(this, URL.controlScope_ok, [pk]);
}

function commonBtnClick(url, pks) {
	let targetKeys = this.state.targetKeys;
	if (!targetKeys || targetKeys.length == 0) {
		this.props.close();
		return;
	}

	let sCtrlScopeNew = [];
	targetKeys.forEach(key => {
		sCtrlScopeNew.push(this.dataKeys[key]);
	});

	ajax({
		url: url,
		data: {
			pks: pks,
			sCtrlScopeNew: sCtrlScopeNew,
		},
		success: res => {
			if (res.success) {
				this.props.close();
			}
		},
	});
}

export default ControlScopeDlg;

/*AyflEomXWyqCHfdTxzWda+ZiNDhxOZJeZF/oshAh0uo=*/